<template>
  <a-drawer
    title="招生目录录入"
    width=450
    placement="right"
    :closable="false"
    @close="onClose"
    :visible="editVisible"
    style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;">
    <a-form :form='form'>
      <a-row :gutter="48">
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="院系">
              <a-select :allowClear="true" showSearch optionFilterProp="children"  v-decorator="[
                  'ssyxm',
                  {
                    rules: [{ required: true, message: '请输入院系!' }],
                  },
                  {initialValue:postParams.ssyxm}
                ]" @change="yxChange" disabled>
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh+'-'+i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="专业">
              <a-select :allowClear="true" showSearch optionFilterProp="children"  v-decorator="[
                  'sszym',
                  {
                    rules: [{ required: true, message: '请输入专业!' }],
                  },
                  {initialValue:postParams.sszym}
                ]" @change="zyChange">
                <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{ i.zyh+'-'+i.zymc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="研究方向">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-decorator="[
                  'yjfxm',
                  {
                    rules: [{ required: true, message: '请输入研究方向!' }],
                  },
                  {initialValue:postParams.yjfxm}
                ]" @change="fxChange">
                <a-select-option v-for="i in selectMenu.fx" :key="i.id" :value="i.yjfxm">{{ i.yjfxm.substring(i.yjfxm.length-2) +"-"+ i.yjfxmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="导师">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-decorator="[
                  'dsxxId',
                  {
                    rules: [{ required: true, message: '请输入导师!' }],
                  },
                  {initialValue:postParams.dsxxId}
                ]"  >
                <a-select-option v-for="i in selectMenu.ds" :key="i.id" :value="i.id">{{ i.gh+'-'+i.xm }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="招生人数">
              <a-input placeholder="请输入..."  v-decorator="[
                  'zsrs',
                  {
                    rules: [{ required: true, message: '请输入招生人数!' }],
                  },
                  {initialValue:postParams.zsrs}
                ]" />
            </a-form-item>
          </a-col>
        <a-col :lg="{span:12}" :sm="{span:12}"  >
          <a-form-item label="排序">
            <a-input placeholder="请输入..." v-decorator="['seq', { rules: [{ required: false, message: '请输入招生人数' }] }]"/>
          </a-form-item>
        </a-col>
          <a-col :sm="{span:24}" :lg="{span:24}"  >
            <a-form-item label="考试科目">
              <a-textarea placeholder="请输入..." v-decorator="[
                  'kskm',
                  {
                    rules: [{ required: true, message: '请输入考试科目!' }],
                  },
                  {initialValue:postParams.kskm}
                ]"  :rows="4"/>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:24}" :lg="{span:24}"  >
            <a-form-item label="备注">
              <a-textarea placeholder="请输入..." v-model="postParams.note" :rows="4"/>
            </a-form-item>
          </a-col>
      </a-row>
    </a-form>
    <div class="drawer-bootom-button">
      <a-button style="margin-right: .8rem" @click="onClose">取消</a-button>
      <a-button @click="handleSubmit" type="primary" :loading="loading">保存</a-button>
    </div>
  </a-drawer>
</template>

<script>
  // import moment from 'moment'
  import {STable} from '~'
  import {mapState} from 'vuex'

  const formItemLayout = {
  labelCol: { span: 3 },
  wrapperCol: { span: 18 }
}

const selectArr = ['TYPE0126', 'TYPE0075', 'TYPE0125', 'TYPE0072']
export default {
  name: 'RecruitAdd',
  props: {
    editVisible: {
      default: false
    },
    info: {
      default: () => []
    }
  },
  components: {
    STable
  },
  data () {
    return {
      formItemLayout,
      loading: false,
      postParams: {
        ssyxm:''
      },
      selectMenu: {
        yx: [], // 院系
        zy: []
      },
      doctoralAdmissionYear:''
    }
  },
  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo,
      TYPE0126: state => state.app['TYPE0126'] // 年级
    })
  },
    beforeCreate() {
      this.form = this.$form.createForm(this)
    },
  created () {

    selectArr.forEach(v => {
      this.$store.dispatch('app/setDictionary', v)
    })
  },
  watch:{
    info(val){
      this.postParams={
        dsxxId:val.dsxxId,
        kskm:val.kskm,
        note:val.note,
        ssyxm:val.ssyxm,
        yjfxm:val.yjfxm,
        zsrs:val.zsrs,
        sszym:val.zyh,
        zsxn:val.zsxn,
        id:val.id,
        seq:val.seq
      }
      this.form.setFieldsValue({'ssyxm':val.ssyxm})
      this.form.setFieldsValue({'yjfxm':val.yjfxm})
      this.form.setFieldsValue({'dsxxId':val.dsxxId})
      this.form.setFieldsValue({'sszym':val.zyh})
      this.form.setFieldsValue({'zsrs':val.zsrs})
      this.form.setFieldsValue({'kskm':val.kskm})
      this.form.setFieldsValue({'seq':val.seq})

      this.initSelectMenu()
    }
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        this.$api.base.majorAll({lsdwh: this.postParams.ssyxm}).then(res => {
        this.$set(this.selectMenu, 'zy', res.data)
        let params={
        ssyxm:this.postParams.ssyxm,
        sszym:this.postParams.sszym,
        xwccm:3,
        zsxn:this.postParams.zsxn
      }
      this.$api.base.researchAll(params).then(res=>{
        this.$set(this.selectMenu,'fx',res.data)
        let param={
        yjfxm:this.postParams.yjfxm,
        zym:this.postParams.sszym,
        zsxn:this.postParams.zsxn,
        ssyxm:this.postParams.ssyxm
      }
      this.$api.base.getGuide(param).then(res=>{
        this.$set(this.selectMenu,'ds',res.data)
      })
      })
      })
      })
       this.$api.base.getSchoolYear().then(res=>{
        this.doctoralAdmissionYear=res.data.doctoralAdmissionYear;
      })



    },
    reset () {
      this.data = []
    },
    // 选择院系
    yxChange (code) {
      this.form.setFieldsValue({'sszym':''})
      this.form.setFieldsValue({'yjfxm':''})
      this.form.setFieldsValue({'dsxxId':''})
      this.$set(this.selectMenu,'zy','')
      this.$set(this.selectMenu,'fx','')
      this.$set(this.selectMenu,'ds','')
      if(code){
       this.$api.base.majorAll({lsdwh: code,zycc:'3'}).then(res => {
        this.$set(this.selectMenu, 'zy', res.data)
      })
      }
    },
    zyChange (code) {
        this.form.setFieldsValue({'yjfxm':''})
      this.form.setFieldsValue({'dsxxId':''})
      this.$set(this.selectMenu,'fx','')
      this.$set(this.selectMenu,'ds','')
      if(code){
      let params={
        ssyxm:this.form.getFieldsValue().ssyxm,
        sszym:code,
        xwccm:3,
        zsxn:this.doctoralAdmissionYear
      }
      this.$api.base.researchAll(params).then(res=>{
        this.$set(this.selectMenu,'fx',res.data)
      })
      }
    },
    //选择方向
    fxChange(code){
        this.form.setFieldsValue({'dsxxId':''})
        this.$set(this.selectMenu,'ds','')
        if(code){
      let params={
        yjfxm:code,
        zym:this.form.getFieldsValue().sszym,
        zsxn:this.doctoralAdmissionYear,
        ssyxm:this.form.getFieldsValue().ssyxm
      }
      this.$api.base.getGuide(params).then(res=>{
        this.$set(this.selectMenu,'ds',res.data)
      })
      let param={

        yjfxm:code,
        ssyxm:this.form.getFieldsValue().ssyxm,
        sszym:this.form.getFieldsValue().sszym
      }
      this.$api.base.getKskm(param).then(res=>{
        this.form.setFieldsValue({'kskm':res.data})
      })
        }
    },
    // 选择招生年度
    zsndChange () {

    },
    // 关闭
    onClose () {
      this.reset()
      this.$emit('close')
    },
    // 保存
    handleSubmit () {


        this.form.validateFields((err, values) => {
          if (err) {
            this.$message.error('请输入必填项')
            return
          }
          var params = Object.assign({id:this.postParams.id,'note':this.postParams.note},this.form.getFieldsValue())
          this.$api.doctorRecruit.CollegeStudentUpate(params).then(res => {

            this.$emit("success");
             this.form.resetFields()
            // this.$refs.stuInfo.form.resetFields()
            // this.$refs.scoreInfo.form.resetFields()
            // this.$refs.stuStatusInfo.form.resetFields()
            // this.$router.push('/student/info/search')
          })
        })
    },
    handleDelete (key) {
      const dataSource = [...this.data]
      this.data = dataSource.filter(item => item.key !== key)
    }
  }
}
</script>
